package com.leyou.mapper;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leyou.DemoApplication;
import com.leyou.pojo.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.sql.Array;
import java.sql.SQLOutput;
import java.util.Arrays;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class UserMapperTest {
    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelect(){
        System.out.println("----- selectAll method test ------");
        List<User> userList = userMapper.selectList(null);
        Assert.assertEquals(5,userList.size());
        userList.forEach(System.out::println);
    }
    @Test
    public void testInsert(){
        User user = new User();
        user.setId(2);
        user.setName("Amy");
        user.setAge(16);
        user.setEmail("amy@itcast.cn");
        userMapper.insert(user);
    }
    //根据条件删除
    @Test
    public void testDelete(){
        int count = userMapper.delete(new QueryWrapper<User>().eq("name", "Amy"));
        System.out.println("count="+count);
    }
    //根据 根据Id删除
    @Test
    public void testDeleteById(){
        int i = userMapper.deleteById(2);
        System.out.println("被删除的id是="+i);
    }
    //根据ID修改
    @Test
    public void testUpdate(){
        User user =new User();
        user.setId(5);
        user.setName("Amy");
        int i = userMapper.updateById(user);
        System.out.println("i = " + i);
    }
    //根据ID查询
    @Test
    public void testSelectById(){
        User user = userMapper.selectById(3);
        System.out.println("user = " + user);
    }
    //根据id们查询
    @Test
    public void testSelectByIdList(){
        List<User> list = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
        list.forEach(System.out::println);
    }

    //根据id们查询
    @Test
    public void testQueryByWrapper(){
        //定义wrapper
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //模糊
        wrapper.like("name","o");
        //查询
        List<User> list = userMapper.selectList(wrapper);

        list.forEach(System.out::println);

    }

    //分页查询
    @Test
    public void testPageQuery(){
        //分页条件
        Page<User> page = new Page<>();
        //当前页
        page.setCurrent(1);
        //每页大小
        page.setSize(3);
        //分页查询，结果会放到Page<User>中，因此无需返回
        userMapper.selectPage(page,null);
        //总条数
        long total = page.getTotal();
        System.out.println("total = " + total);
        //总页数
        long pages = page.getPages();
        System.out.println("pages = " + pages);
        //当前页结果
        List<User> list = page.getRecords();
        list.forEach(System.out::println);

    }

}
